<?php

class XDb {
	public $querysCount, $lastID;
	
	private $_connectionActive;
	
	public function __construct () {
		if(!XT::$registry->storage->_connectionActive):
			$data = explode(':', XT::$registry->config->site['db']);
		
			mysql_connect($data[0], $data[1], $data[2]);
			mysql_select_db($data[3]);
			mysql_set_charset('utf8');
			
			XT::$registry->storage->_connectionActive = true;
		endif;
	}
	
	public function query ($query, $replaces=array()) {
		if(!empty($replaces)) {
			foreach ($replaces as $key => $value) {
				$query = str_replace($key, $value, $query);
			}
		}
		
		if(($response=mysql_query($query))===true)
			XT::$registry->storage->querysCount++;
			
		if(strstr('INSERT', $query)) {
			XT::$registry->storage->lastID = mysql_insert_id();
			$this->lastID = mysql_insert_id();
		}
			
		return $response;
	}
}